/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.HanhTrinhDTO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;

/**
 *
 * @author Nobita Chau
 */
public class HanhTrinhDAO {
    
        public static boolean Them(HanhTrinhDTO n) {
        try {
            Connection conn = DBConnect.Connect();
            Statement statement = conn.createStatement();
            String sql = String.format ("INSERT INTO `quanlyduongsat`.`hanh_trinh` (`tuyen`, `gadi`, `gaden`, `thoi_gian_di`, `thoi_gian_nghi`, `don_vi_tinh`) "
                    + "VALUES ('%s', '%d', '%d', '%f', '%f', '%s')",n.Tuyen,n.GaDi,n.GaDen,n.ThoiGianDi,n.ThoiGianNghi,n.DonViTinh);
            statement.executeUpdate(sql);
            conn.close();
            return true;
           // JOptionPane.showMessageDialog(null, "Thêm thành công!!!", "Thông báo", 1);
        } catch (Exception ex) {
           // JOptionPane.showMessageDialog(null, "Thêm Không thành công!!!", "Thông báo", 1);
        }
        return false;
    }

    public static boolean Xoa(int id) {
        try {
            Connection conn = DBConnect.Connect();
            String strSQL = "delete from hanh_trinh where id = ?";
            PreparedStatement pst = conn.prepareStatement(strSQL);
            pst.setInt(1, id);
            pst.executeUpdate();
            //JOptionPane.showMessageDialog(null, "Xóa thành công!!!", "Thông báo", 1);
            pst.close();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            ex.getMessage();
           // JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
        }
        return false;
    }
    
    public static ResultSet TimKiemTatCa() {
        try {
            Connection conn = DBConnect.Connect();
            String sql = "Select ht.ID,ht.TUYEN,g1.TEN as GaDi,g2.TEN as GaDen,ht.THOI_GIAN_DI,ht.THOI_GIAN_NGHI,ht.DON_VI_TINH\n" +
"from HANH_TRINH ht join GA g1 on ht.GADI=g1.ID join GA g2 on ht.GADEN=g2.ID ";
            Statement st = conn.createStatement();
            ResultSet rs = st.executeQuery(sql);
          //  conn.close();
            return rs;

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getStackTrace(), "Thông báo lỗi", JOptionPane.OK_OPTION);
            return null;
        }

    }
      public static boolean KiemTraTonTai(int MaTuyen)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "Select * From hanh_trinh Where id like ?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setInt(1, MaTuyen);
            
            ResultSet rs = ps.executeQuery();
            
            if (rs.next())
                return true;
        }
        catch (Exception ex)
        {
        }
        return false;
    }
       public static boolean Update(HanhTrinhDTO C, int id)
    {
        try
        {
            Connection con = DBConnect.Connect();
            String sql = "UPDATE `quanlyduongsat`.`hanh_trinh` SET `tuyen`=?, `gadi`=?, `gaden`=?, `thoi_gian_di`=?, `thoi_gian_nghi`=?, `don_vi_tinh`=? WHERE `id`=?";
            
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, C.Tuyen);
            ps.setInt(2, C.GaDi);
            ps.setInt(3, C.GaDen);
            ps.setDouble(4, C.ThoiGianDi);
            ps.setDouble(5, C.ThoiGianNghi);
            ps.setString(6,C.DonViTinh);
            ps.setInt(7,id);
            int n = ps.executeUpdate();
            con.close();
            
            if (n == 1)
                return true;
        }
        catch (Exception ex)
        {
             System.err.println(ex.getMessage());
        }
        return false;
    }
}
